package com.useeinfo.oa.open.admin.wuHanCommon;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.action.BaseAction;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.AutomaticTableBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WarningBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WarningCreateAcceptBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WarningCreateAccept;
import com.useeinfo.oa.modules.wuHanCommon.base.result.ResultDto_Wh;
import com.useeinfo.oa.modules.wuHanCommon.base.result.ResultUtil_Wh;
import com.useeinfo.oa.modules.wuHanCommon.base.vo.WarningVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

@Controller
@RequestMapping("/web/wuhan/warning")
public class WarningAction extends BaseAction {

    @Autowired
    private WarningCreateAcceptBiz warningCreateAcceptBiz;
    @Autowired
    private WarningBiz warningBiz;
    @Autowired
    private AutomaticTableBiz automaticTableBiz;

    /**
     * 分页获取JSON数据
     */
    @RequestMapping("/getWarningListJSON")
    @ResponseBody
    public JSONObject getWarningListJSON(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                         @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                         @RequestParam(value = "orderBy", required = false) String orderBy,
                                         @RequestParam(value = "searchCode", required = false) String searchCode) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("searchCode", searchCode);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        return warningBiz.findJSONList(queryParam);
    }


    /**
     * 自动抄表
     */
    /*@RequestMapping(value = "/set_realtimedata_from_site", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto_Wh set_realtimedata_from_site(@RequestBody WarningRealtimeVO warningRealtimeVO) {
        logger.info("自动抄表对接的json：{}", JSONObject.toJSONString(warningRealtimeVO));
        ResultDto_Wh resultDto_wh = automaticTableBiz.setRealTimeDataFromSite(warningRealtimeVO);
        warningCreateAcceptBiz.addOrUpdate(new WarningCreateAccept(JSONObject.toJSONString(warningRealtimeVO), resultDto_wh.getErr(), resultDto_wh.getMsg(), warningRealtimeVO.getProjId(), warningRealtimeVO.getToken(), 1));
        return resultDto_wh;
    }*/

    /**
     * 自动抄表
     */
    @RequestMapping(value = "/set_realtimedata_from_site", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto_Wh set_realtimedata_from_site(HttpServletRequest request) {
        try {
            BufferedReader br = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line); //这里需要注意，只是给出读取示例，这样做会在最后一行多加一个换行符，建议用字节来读
            }
            String resultXML = sb.toString();
            JSONObject data = JSONObject.parseObject(resultXML);
            logger.info("自动抄表:{}", data);
            ResultDto_Wh resultDto_wh = automaticTableBiz.setRealTimeDataFromSite(data);
            warningCreateAcceptBiz.addOrUpdate(new WarningCreateAccept(data.toJSONString(), resultDto_wh.getErr(), resultDto_wh.getMsg(), null, null, 1));
            return resultDto_wh;
        } catch (Exception e) {
            return ResultUtil_Wh.error("数据异常", 0);
        }
    }

    /**
     * 报警通知接口
     * Content-Type: application/json
     * method：POST
     */
    @RequestMapping(value = "/create", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto_Wh warningCreate(@RequestBody WarningVO warningVO) {
        logger.info("报警通知接口对接的json：{}", JSONObject.toJSONString(warningVO));
        if (warningVO == null) {
            return ResultUtil_Wh.error("无数据");
        }
      /*  if (warningCreateAcceptBiz.isExist(warningVO.getProjId(), warningVO.getToken())) {
            return ResultUtil_Wh.success();
        }*/
        //开始解析数据
        ResultDto_Wh resultDto_wh = warningBiz.warningCreate(warningVO.getWarningList());
        warningCreateAcceptBiz.addOrUpdate(new WarningCreateAccept(JSONObject.toJSONString(warningVO), resultDto_wh.getErr(), resultDto_wh.getMsg(), warningVO.getProjId(), warningVO.getToken(), 0));
        return resultDto_wh;
    }

    /**
     * 分页获取JSON数据
     */
    @RequestMapping("/message")
    @ResponseBody
    public Boolean message() {
        return warningCreateAcceptBiz.isWarmingMessage();
    }

}
